package com.xiaxueqi.web.portal;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.niagara.context.SecurityContext;
import com.niagara.dao.Page;
import com.niagara.security.auth.entity.UserDetail;
import com.niagara.service.base.BaseService;
import com.niagara.web.struts2.Struts2Utils;
import com.xiaxueqi.entity.book.AppOrder;
import com.xiaxueqi.entity.book.BookForsale;
import com.xiaxueqi.entity.book.BookForsaleExt;
import com.xiaxueqi.entity.security.AppUser;
import com.xiaxueqi.entity.security.AppUserExt;
import com.xiaxueqi.service.BookDTOService;
import com.xiaxueqi.service.UserDTOService;

public class IndexAction {

	@Autowired
	private BaseService baseService;

	@Autowired
	private BookDTOService bookDTOService;

	@Autowired
	private UserDTOService userDTOService;

	private List<BookForsaleExt> latestSaleList; // 最新上架图书
	private List<AppOrder> latestOrderList; // 最新发布需求
	private List<AppUserExt> popUserList;// 最受欢迎书友
	private List<BookForsaleExt> popBookList;// 图书排行榜
	private List<BookForsaleExt> recBookList;// 强力推荐图书
	private List<BookForsaleExt> cheBookList;// 大折扣区图书
	private String loginId = this.setLoginId();

	// 默认执行，首页上信息的提取
	@SuppressWarnings("unchecked")
	public String execute() throws Exception {
		Map map = Struts2Utils.getParameters();

		// 最新上架
		Page<BookForsale> tempPager = this.baseService.findForPaging(
				BookForsale.class, map, 1, 5);
		if (tempPager == null) {
			latestSaleList = new ArrayList<BookForsaleExt>();
		} else {
			latestSaleList = this.bookDTOService.getBookDTOPage(tempPager)
					.getPageList();
		}
		// 最新需求
		Page<AppOrder> orderPager;
		orderPager = this.baseService.findForPaging(AppOrder.class, map, 1, 10);
		latestOrderList = orderPager != null ? orderPager.getPageList()
				: new ArrayList<AppOrder>();

		// 图书排行榜
		Page<BookForsale> popBookPager;
		popBookPager = this.baseService.findForPaging(BookForsale.class, null,
				1, 10);
		List<BookForsale> tempPopBookList = popBookPager != null ? popBookPager
				.getPageList() : new ArrayList<BookForsale>();
		this.popBookList = this.bookDTOService.getBookDTOList(tempPopBookList);

		// 最受欢迎书友
		Page<AppUser> popUserPager;
		popUserPager = this.baseService.findForPaging(AppUser.class, map, 1, 5);
		List<AppUser> tempPopUserList = popUserPager != null ? popUserPager
				.getPageList() : new ArrayList<AppUser>();
		this.popUserList = this.userDTOService
				.getAppUserDTOList(tempPopUserList);

		// 强力推荐图书
		Page<BookForsale> recBookPager;
		recBookPager = this.baseService.findForPaging(BookForsale.class, map,
				1, 5);
		List<BookForsale> tempRecBookList = recBookPager != null ? recBookPager
				.getPageList() : new ArrayList<BookForsale>();
		recBookList = this.bookDTOService.getBookDTOList(tempRecBookList);

		// 大折扣区图书
		Page<BookForsale> cheBookPager;
		cheBookPager = this.baseService.findForPaging(BookForsale.class, map,
				1, 5);
		List<BookForsale> tempCheBookList = cheBookPager != null ? cheBookPager
				.getPageList() : new ArrayList<BookForsale>();
		cheBookList = this.bookDTOService.getBookDTOList(tempCheBookList);

		return "success";
	}

	// 用于搜索框的搜索
	public String search() throws Exception {

		return "success";
	}

	public List<BookForsaleExt> getLatestSaleList() {
		return latestSaleList;
	}

	public List<AppOrder> getLatestOrderList() {
		return latestOrderList;
	}

	public List<AppUserExt> getPopUserList() {
		return popUserList;
	}

	public List<BookForsaleExt> getPopBookList() {
		return popBookList;
	}

	public List<BookForsaleExt> getRecBookList() {
		return recBookList;
	}

	public List<BookForsaleExt> getCheBookList() {
		return cheBookList;
	}

	public String getLoginId() {
		return loginId;
	}

	private String setLoginId() {
		UserDetail detail = SecurityContext.getUserDetail();
		String tempLoginId = detail != null ? detail.getLoginId() : null;
		return tempLoginId;
	}
}
